package es.index.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 锁定文件夹
 * 
 * @author BeiBei
 */
public final class LockFolder {
	private List<LockFile> _fileLocks = null;

	private String _file_path = "";

	private char _separatorChar = '\\';

	/**
	 * 获取锁定的文件集合
	 * 
	 * @return
	 */
	public List<LockFile> getLockFiles() {
		return _fileLocks;
	}

	@SuppressWarnings("static-access")
	public LockFolder(File file) {
		_file_path = file.getAbsolutePath();
		_separatorChar = file.separatorChar;
		_fileLocks = new ArrayList<LockFile>();

		if (file.isDirectory()) {
			lockFile(file.listFiles());
		} else {
			_fileLocks.add(new LockFile(file));
		}
	}

	/**
	 * 创建指定文件名称的文件
	 * 
	 * @param fileName
	 */
	public void createFile(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				return;
			}
		}
		File file = new File(_file_path + _separatorChar + fileName);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		_fileLocks.add(new LockFile(file));
	}

	/**
	 * 根据文件名称删除文件
	 * 
	 * @param fileName
	 */
	public void removeFile(String fileName) {
		int i = _fileLocks.size();
		String path = "";
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				path = _fileLocks.get(i).getAbsolutePath();
				_fileLocks.get(i).release();
				_fileLocks.get(i).close();
				break;
			}
		}

		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	/**
	 * 查找是否具有指定名称的文件
	 * 
	 * @param fileName
	 * @return
	 */
	public boolean search(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 释放所有独占访问的线程
	 */
	public void release() {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			_fileLocks.get(i).release();
		}
	}

	/**
	 * 根据文件名称释放独占访问的线程
	 */
	public void release(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				_fileLocks.get(i).release();
				break;
			}
		}
	}

	/**
	 * 关闭所有独占访问的线程
	 */
	public void close() {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			_fileLocks.get(i).close();
			_fileLocks.remove(i);
		}
	}

	/**
	 * 根据文件名称关闭独占访问的线程
	 */
	public void close(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				_fileLocks.get(i).close();
				_fileLocks.remove(i);
				break;
			}
		}
	}

	/**
	 * 根据文件名称往一个文件中写入数据
	 * 
	 * @param value
	 * @param fileName
	 */
	public void write(String value, String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				_fileLocks.get(i).write(value);
				break;
			}
		}
	}

	public void delete(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				_fileLocks.get(i).delete();
				_fileLocks.remove(i);
				break;
			}
		}
	}

	/**
	 * 根据文件名称读取一个文件中的数据
	 * 
	 * @param value
	 * @param fileName
	 */
	public String readLine(String fileName) {
		int i = _fileLocks.size();
		for (; --i >= 0;) {
			if (_fileLocks.get(i).getFileName().equals(fileName)) {
				return _fileLocks.get(i).readLine();
			}
		}
		return "";
	}

	private void lockFile(File[] files) {
		for (File file : files) {
			if (file.isDirectory()) {
				lockFile(file.listFiles());
			} else {
				_fileLocks.add(new LockFile(file));
			}
		}
	}
}
